package org.ling.xuan.action;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.intellij.debugger.DebuggerManagerEx;
import com.intellij.debugger.impl.DebuggerSession;
import com.intellij.debugger.ui.HotSwapUI;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ModuleRootManagerEx;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.packaging.artifacts.Artifact;
import com.intellij.packaging.artifacts.ArtifactManager;
import com.intellij.packaging.impl.artifacts.workspacemodel.ArtifactManagerBridge;
import com.intellij.task.ProjectTaskManager;
import lombok.SneakyThrows;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.concurrency.Promise;
import org.ling.xuan.dialog.TestDialog;
import org.ling.xuan.util.LoggerUtil;
import org.ling.xuan.util.LxStrUtil;


public class LxTestAction extends AnAction {

    @SneakyThrows
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        TestDialog testDialog = new TestDialog(e.getProject(), true);
        if (testDialog.showAndGet()) {
            if (StrUtil.isNotBlank(testDialog.getInput())) {
                String s = testDialog.getInput();
                LoggerUtil.log(e.getProject(), "debug:{}", s);
                switch (s) {
                    case "1":
                        String fs = "D:/svn-app/ifmonline";
                        ProjectManager.getInstance().loadAndOpenProject(fs);
                        break;
                    case "2":
                        ModuleManager moduleManager = ModuleManager.getInstance(e.getProject());
                        Module[] modules = moduleManager.getModules();
                        for (int i = 0; i < modules.length; i++) {
                            LoggerUtil.log(e.getProject(), modules[i].getName());
                            ModuleRootManager rootManager = ModuleRootManager.getInstance(modules[i]);
                            ModifiableRootModel modifiableModel = rootManager.getModifiableModel();
                            ModuleRootManagerEx moduleRootManagerEx = ModuleRootManagerEx.getInstanceEx(modules[i]);
                        }
                        break;
                    case "3":
                        ArtifactManager artifactManager = ArtifactManagerBridge.getInstance(e.getProject());
                        Artifact[] artifacts = artifactManager.getSortedArtifacts();
                        for (int i = 0; i < artifacts.length; i++) {
                            LoggerUtil.log(e.getProject(), artifacts[i].getName());
                            LoggerUtil.log(e.getProject(), artifacts[i].getArtifactType().getPresentableName());
                            LoggerUtil.log(e.getProject(), artifacts[i].getArtifactType().getId());
                            LoggerUtil.log(e.getProject(), artifacts[i].getOutputFilePath());
                            LoggerUtil.log(e.getProject(), artifacts[i].getOutputPath());
                        }
                        break;
                    case "4":
                        DebuggerManagerEx debuggerManager = DebuggerManagerEx.getInstanceEx(e.getProject());
                        DebuggerSession session = debuggerManager.getContext().getDebuggerSession();
                        if (session != null && session.isAttached()) {
                            HotSwapUI.getInstance(e.getProject()).reloadChangedClasses(session, true);
                        } else {
                            LoggerUtil.log(e.getProject(), "no debug Session");
                        }
                        break;
                    case "5":
                        String filePath = "D:\\workspace\\test\\com.liting.demo\\com\\liting\\demo\\demo\\SqlDemo.java";
                        LxStrUtil.unionPath(filePath);
                        if (filePath.endsWith(".java")) {
                            VirtualFile virtualFile = VirtualFileManager.getInstance().findFileByUrl("file://" + filePath);
                            Promise<ProjectTaskManager.Result> resultPromise = ProjectTaskManager.getInstance(e.getProject()).compile(virtualFile);
                            while (resultPromise.isSucceeded()) {
                                LoggerUtil.log(e.getProject(), "compiler success");
                            }
                        }
                        break;
                    case "6":
                        Project project = e.getProject();
                        ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
                        LoggerUtil.log(e.getProject(), JSONUtil.toJsonPrettyStr(projectRootManager.getContentRootUrls()));
                        break;
                    default:
                        LoggerUtil.log(e.getProject(), "\n1.打开一个项目\n2.查看module信息\n3.查看artifact信息\n4.查看debug信息\n5.尝试调试文件\n6.查看项目信息");
                }
            }
        }
    }


}
